/**
 * 
 */
package cc.monggo.demo.thread;

/**
 * @author Administrator
 * 
 */

public class ThreadLocalDemo {
	private static ThreadLocal seqNum = new ThreadLocal() {
		public Integer initialValue() {
			return 0;
		}
	};

	public int getNextNum() {
		seqNum.set((Integer) seqNum.get() + 1);
		return (Integer) seqNum.get();
	}

	public static void main(String[] args) {
		ThreadLocalDemo sn = new ThreadLocalDemo();
		TestClient t1 = new TestClient(sn);
		TestClient t2 = new TestClient(sn);
		TestClient t3 = new TestClient(sn);
		t1.start();
		t2.start();
		t3.start();
	}

	private static class TestClient extends Thread {
		private ThreadLocalDemo sn;

		public TestClient(ThreadLocalDemo sn) {
			this.sn = sn;
		}

		public void run() {
			for (int i = 0; i < 3; i++) {
				System.out.println("thread[" + Thread.currentThread().getName()
						+ "] sn[" + sn.getNextNum() + "]");
			}
		}
	}
}
